﻿
using SqlSugar;
using System;
using System.Linq;

namespace drinking.Helpers
{
    public class DBContext
    {
        private string connectionString = "Database=drinking; Data Source=39.104.59.59;port=3306;User Id=root;Password=drink@0308; charset=utf8;";
        private DbType dbType = DbType.MySql;

        public static string DB_ConnectionString { get; set; } = "Database=drinking; Data Source=39.104.59.59;port=3306;User Id=root;Password=drink@0308; charset=utf8;";

        public static DbType DB_dbType { get; set; } = DbType.MySql;


        public DBContext() : this(DB_dbType, DB_ConnectionString)
        {

        }


        public DBContext(DbType DB_dbType, string DB_ConnectionString)
        {
            //this.connectionString = connectionString;
            this.connectionString = DB_ConnectionString;
            this.dbType = DB_dbType;
            this.Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = this.connectionString,
                DbType = this.dbType,
                //DbType = dbType,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            });
            //调式代码 用来打印SQL 
            this.Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" +
                    this.Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
            this.Db.Ado.IsEnableLogEvent = true;
        }
        public SqlSugarClient Db { get; private set; }
    }
}
